package com.etc.order.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.etc.order.common.domain.OrderDetail;
import com.etc.order.common.domain.OrderMaster;
import com.etc.order.common.service.OrderMasterService;
import com.etc.order.provider.mapper.OrderDetailMapper;
import com.etc.order.provider.mapper.OrderMasterMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class OrderMasterServiceImpl implements OrderMasterService {
    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public boolean createOrderMaster(OrderMaster order, List<OrderDetail> items) {
        int t1 = orderMasterMapper.createOrder(order);
        int t2 = 0;
        for (OrderDetail detail : items) {
            t2 += orderDetailMapper.createOrderDetail(detail);
        }
        return t1 > 0 && t2 > 0;
    }

    @Override
    public List<OrderMaster> findOrderMasterListByName(String buyerName, int pageIndex, int pageSize) {
        return orderMasterMapper.findByBuyerName(buyerName, pageIndex, pageSize);
    }

    @Override
    public OrderMaster findByOrderId(String orderId) {
        return orderMasterMapper.getOne(orderId);
    }

    @Override
    public List<OrderMaster> findAll(int pageIndex, int pageSize) {
        return orderMasterMapper.findAll(pageIndex, pageSize);
    }

    @Override
    public int getTotalCount(String buyerName) {
        return orderMasterMapper.getTotalCount(buyerName);
    }

    @Override
    public int update(OrderMaster orderMaster) {
        return orderMasterMapper.update(orderMaster);
    }
}
